package zmee.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.util.HttpClientUtils;

import java.util.HashMap;

public class GetCwgxAccessTokenUtil {
    private static Log logger = LogFactory.getLog(GetCwgxAccessTokenUtil.class);

    static String accountId = DBUtil.getParamValue("gxaccountId");
   static String tenantid = DBUtil.getParamValue("gxtenantid");



    /**
     * 获取apptoken
     *
     * @return
     */
    public static String getApptoken() {
        String apptoken = "";
        String appId = "SAP_API";
        String appSecuret = "Kdadminopenapi11";
        String language = "zh_CN";

        String url = DBUtil.getParamValue("CQUrl");
        url = url + "/api/getAppToken.do";

        JSONObject jb = new JSONObject();
        jb.put("appId", appId);
        jb.put("appSecuret", appSecuret);
        jb.put("tenantid", tenantid);
        jb.put("accountId", accountId);
        jb.put("language", language);
        logger.info("GetCwgxAccessTokenUtil-入参："+jb);
        String result = "";
        HashMap<String, String> header = new HashMap<>();
        header.put("Content-Type","application/json");
        try {
            result = HttpRequestUtil.getResult(url, jb.toJSONString());
            logger.info("GetCwgxAccessTokenUtil--结果："+result);
            HashMap hashMap = JSON.parseObject(result, HashMap.class);
            if ("success".equalsIgnoreCase((String) hashMap.get("state"))) {
                JSONObject data = (JSONObject) hashMap.get("data");
                if (data.getBooleanValue("success")) {

                    apptoken = data.getString("app_token");

                }
            }
        } catch (Exception e) {
        }
        return apptoken;

    }

    /**
     * 获取accesstoken
     *
     * @return
     */
    public static String getAccesstoken(String userPhone) {
        String accesstoken = "";
        String user = userPhone;// 用户名
        String apptoken = getApptoken();// apptoken
        String usertype = "Mobile";// 用户登陆方式，默认手机

        String url = DBUtil.getParamValue("CQUrl");// ip
        url = url + "/api/login.do";// api
        // 将参数拼接json格式
        JSONObject jb = new JSONObject();
        jb.put("user", user);
        jb.put("apptoken", apptoken);
        jb.put("accountId", accountId);
        jb.put("tenantid", tenantid);
        jb.put("usertype", usertype);
        jb.put("language","zh_CN");
        String result = "";
        try {
            HashMap<String, String> header = new HashMap<>();
            header.put("Content-Type","application/json");
            result = HttpRequestUtil.getResult(url, jb.toJSONString());
            logger.info("GetCwgxAccessTokenUtil--"+result);

            HashMap hashMap = JSON.parseObject(result, HashMap.class);
            // 接口调用成功
            if ("success".equalsIgnoreCase((String) hashMap.get("state"))) {
                JSONObject data = (JSONObject) hashMap.get("data");
                // 成功返回数据
                if (data.getBooleanValue("success")) {

                    accesstoken = data.getString("access_token");
                }
            }
        } catch (Exception e) {
        }
        return accesstoken;

    }
}
